<?
define('SCOT_SOURCE_BLOG_NAME', 01);
define('SCOT_SOURCE_SITE_URL', 02);
define('SCOT_SOURCE_TAGCLOUD_ID', 03);
define('SCOT_SOURCE_USER_NAME', 11);
define('SCOT_SOURCE_POST_TITLE', 21);
define('SCOT_SOURCE_POST_DATE', 22);
define('SCOT_SOURCE_TAG_NAME', 31);
define('SCOT_SORUCE_TAG_ID', 32);

class Source {

	var $db = null;
	var $userList = null;
	var $userPostList = null;
	var $postTagList = null;
	
	function getDB() {
		if(!isset($db)) {
			$db = new DB();
		}
		return $db;
	}
		
	function getUserList() {
		$dbc = $this->getDB();
		
		$this->userList = array();

		$userCount = $dbc->query("SELECT ID FROM ".DB_PREFIX."users;");

		for($i = 0 ; $i < $userCount ; $i++) {
			$this->userList[] = $dbc->get_var(null, 0, $i);
		}

		return $this->userList;
	}
	
	function getUserPostList($userid) {
		$dbc = $this->getDB();
		
		$this->userPostList = array();
		
		$userPostCount = $dbc->query("SELECT ID FROM ".DB_PREFIX."posts WHERE post_author = '$userid';");
		
		for($i = 0 ; $i < $userPostCount ; $i++) {
			$this->userPostList[] = $dbc->get_var(null, 0, $i);
		}
		
		return $this->userPostList;
	}
	
	function getTagPostList($tagid) {
		$dbc = $this->getDB();
		
		$this->tagPostList = array();

		$tagPostCount = $dbc->query("SELECT post_id FROM ".DB_PREFIX."post2tag where tag_id = '$tagid' ORDER BY post_id;");

		for($i = 0 ; $i < $tagPostCount ; $i++) {
			$this->tagPostList[] = $dbc->get_var(null, 0, $i);
		}
		
		return $this->tagPostList;
	}
	
	function getPostTagList($postid) {
		$dbc = $this->getDB();
		
		$this->postTagList = array();

		$postTagCount = $dbc->query("SELECT tag_id FROM ".DB_PREFIX."post2tag where post_id = '$postid' ORDER BY tag_id;");

		for($i = 0 ; $i < $postTagCount ; $i++) {
			$this->postTagList[] = $dbc->get_var(null, 0, $i);
		}
		
		return $this->postTagList;			
	} 
	
	function getInfo($infoName, $id) {
		$info = null;
		
		$dbc = $this->getDB();
		
		switch($infoName) {
			case SCOT_SOURCE_BLOG_NAME : {
				$info = $dbc->get_var("SELECT option_value FROM ".DB_PREFIX."options WHERE option_name = 'blogname'", 0, 0);
				break;
			}
			case SCOT_SOURCE_SITE_URL : {
				$info = $dbc->get_var("SELECT option_value FROM ".DB_PREFIX."options WHERE option_name = 'siteurl'", 0, 0);
				break;
			}
			case SCOT_SOURCE_TAGCLOUD_ID : {
				$info = "/".$id;//.date('YmdHis',time());
				break;
			}
			case SCOT_SOURCE_USER_NAME : {
				$info = $dbc->get_var("SELECT user_nicename FROM ".DB_PREFIX."users WHERE ID = '$id'", 0, 0);
				break;
			}
			case SCOT_SOURCE_POST_TITLE : {
				$info = $dbc->get_var("SELECT post_title FROM ".DB_PREFIX."posts WHERE ID = '$id'", 0, 0);
				break;
			}
			case SCOT_SOURCE_POST_DATE : {
				$info = $dbc->get_var("SELECT post_date FROM ".DB_PREFIX."posts where ID = '$id'", 0, 0);
				break;
			}
			case SCOT_SOURCE_TAG_NAME : {
				$info = $dbc->get_var("SELECT tag FROM ".DB_PREFIX."tags WHERE tag_ID = '$id'", 0, 0);
				break;
			}
			case SCOT_SORUCE_TAG_ID : {
				$info = $dbc->get_var("SELECT tag_ID FROM ".DB_PREFIX."tags WHERE tag = '$id'", 0, 0);
				break;
			}
			default : {
				echo('Exception : Invalid InfoName Exception : getUserInfo('.$infoName.')<br>');
			}
		}
		return $info;
	}
	
	function getDateFormat() {
		return 'Y-m-d h:i:s';
	}
}

?>